Add a `repo()` accessor to `TransactionGuard`
authorColin Walters <walters@verbum.org>
Tue, 26 Apr 2022 23:30:01 +0000 (19:30 -0400)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:57 +0000 (12:53 -0400)
I want to write APIs that *require* the caller to have set up
an ostree transaction.  It's natural to require passing a guard
to do so.  But then we want an accessor for the repo.

rust-bindings/src/repo.rs
rust-bindings/tests/util/mod.rs

index 35a15248a98e1dbd01e217165d709a187afd45b3..5d34f98b44f55730b9cea7a1aea54a909ab3619b 100644 (file)
@@ -72,6 +72,12 @@ pub struct TransactionGuard<'a> {
 }
 
 impl<'a> TransactionGuard<'a> {
+    /// Returns a reference to the repository.
+    pub fn repo(&self) -> &Repo {
+        // SAFETY: This is only set to None in `commit`, which consumes self
+        self.repo.unwrap()
+    }
+
     /// Commit this transaction.
     pub fn commit<P: IsA<gio::Cancellable>>(
         mut self,
index 472bf4553e2862e087c290d019fb13dab0de2411..28cca92694df68d8791d4b53f4583f14af81281e 100644 (file)
@@ -67,7 +67,8 @@ pub fn commit(repo: &ostree::Repo, mtree: &ostree::MutableTree, ref_: &str) -> G
     let txn = repo
         .auto_transaction(NONE_CANCELLABLE)
         .expect("prepare transaction");
-    let repo_file = repo
+    let repo_file = txn
+        .repo()
         .write_mtree(mtree, NONE_CANCELLABLE)
         .expect("write mtree")
         .downcast::<ostree::RepoFile>()